package portal.monitor.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import portal.common.MesageCode;
import portal.common.ResultData;
import portal.monitor.dao.BlackList;
import portal.monitor.service.BlackListService;

@RestController
@RequestMapping(value="/ws/remote/")
public class BlacklistAction extends BaseActioin {
	
    @Autowired
	BlackListService blackListService;
	
	
	@RequestMapping(value="/blacklist/add",method=RequestMethod.POST)
	public ResultData add(BlackList blacklist){
		System.out.println("*****"+blacklist.getAppName());
		ResultData result = new ResultData();
		int count = blackListService.add(blacklist);
		if(count>0){
			result.setResultCode(MesageCode.SUCCESS);
			result.setData("添加成功");
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setData("添加失败");
		return result;
	}
	
	@RequestMapping(value="/blacklist/remove",method=RequestMethod.GET)
	public ResultData remove(Integer id){
		System.out.println("*****"+id);
		ResultData result = new ResultData();
		int count = blackListService.delete(id);
		if(count>0){
			result.setResultCode(MesageCode.SUCCESS);
			result.setData("删除成功");
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setData("删除失败");
		return result;
	}
	

	@RequestMapping(value="/blacklist/update",method=RequestMethod.POST)
	public ResultData update(BlackList blacklist){

		System.out.println("*****"+blacklist.getId());
		ResultData result = new ResultData();
		int count = blackListService.update(blacklist);
		if(count>0){
			result.setResultCode(MesageCode.SUCCESS);
			result.setData("更新成功");
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setData("更新失败");
		return result;
			
	}
	
	@RequestMapping(value="/blacklist/get",method=RequestMethod.GET)
	public ResultData get(Integer id){

		ResultData result = new ResultData();
		BlackList blacklist = blackListService.get(id);
		if(blacklist!=null){
			result.setResultCode(MesageCode.SUCCESS);
			result.setDesc("SUCCESS");
			result.setData(blacklist);
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setDesc("ERROR");
		result.setData("无该ID的记录");
		return result;
	}
	
	@RequestMapping(value="/blacklist/list",method=RequestMethod.GET)
	public ResultData list(BlackList blacklist){
	
		ResultData result = new ResultData();
		List<BlackList> list = blackListService.list(blacklist);
		if(list!=null){
			result.setResultCode(MesageCode.SUCCESS);
			result.setData(list);
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setData("无该条件的记录");
		return result;
	}
	
	@RequestMapping(value="/blacklist/listpage",method=RequestMethod.GET)
	public ResultData listpage(BlackList blacklist,Integer page,Integer pageSize){

		ResultData result = new ResultData();
		PageHelper.startPage(page,pageSize);
		List<BlackList> list = blackListService.list(blacklist);
		PageInfo pageInfo = new PageInfo(list);
		if(list!=null){
			result.setResultCode(MesageCode.SUCCESS);
			result.setData(pageInfo);
			result.setMessage(pageInfo.getList().size());
			return result;
		}
		
		result.setResultCode(MesageCode.ERROR);
		result.setData("无该条件的记录");
		result.setMessage(0);
		return result;
	}

}
